package com.example.oversea_home_spring.controller;


import com.example.oversea_home_spring.bean.Management;
import com.example.oversea_home_spring.bean.Order;
import com.example.oversea_home_spring.bean.Warehouse;
import com.example.oversea_home_spring.service.TraderService;
import com.example.oversea_home_spring.service.WorkerService;
import com.example.oversea_home_spring.uitl.CodeMsg;
import com.example.oversea_home_spring.uitl.DateUtil;
import com.example.oversea_home_spring.uitl.Result;
import com.example.oversea_home_spring.uitl.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//工人
@RestController
@CrossOrigin//跨域
@RequestMapping("worker")
public class WorkerController {

    @Autowired
    TraderService traderService;

    @Autowired
    WorkerService workerService;

    // 待处理的订单 工人id
    @GetMapping("allOrder")
    public Result<List<Order>> selectAllOrder(String workerId){
        List<Order> list = workerService.selectAllOrder(workerId);
        if(list!=null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.SELECT_ORDER_ERROR);
    }
   //我处理的订单  工人id
   @GetMapping("myOrder")
    public Result<List<Order>> selectMyOrder(String workerId){
        List<Order> list = workerService.selectMyOrder(workerId);
        if(list!=null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.SELECT_ORDER_ERROR);
    }

    //订单处理
    @PutMapping("manageOrder")
    public Result<Integer> updateOrder(Order order){
        order.setUpdate_time(DateUtil.myDate());
        int isOk = workerService.updateOrder(order);
        if(isOk > 0){
            return Result.success(isOk);
        }
        return Result.error(CodeMsg.UPDATE_ORDER_ERROR);
    }

    //所有仓库
    @GetMapping("allWarehouse")
    public Result<List<Warehouse>> selectAllHouse(){
        List<Warehouse> list = traderService.selectAllHouse();
        if(list != null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.SELECT_HOUSE_ERROR);
    }

    //加入仓库工作
    @PutMapping("entry")
    public Result<Integer> addManagement(Management management){
        management.setStatus((long)1);
        management.setId(UUIDUtil.uuid());
        management.setCreate_time(DateUtil.myDate());
        management.setUpdate_time(DateUtil.myDate());
        int isOk = workerService.addManagement(management);
        if(isOk > 0){
            return Result.success(isOk);
        }
        return Result.error(CodeMsg.ADD_MANAGE_ERROR);
    }
    //辞职
    @PutMapping("resign")
    public Result<Integer> updateManagementStatus(String managementId){
        int isok = workerService.updateManagementStatus(managementId);
        if(isok > 0){
            return Result.success(isok);
        }
        return Result.error(CodeMsg.UPDATE_MANAGE_ERROR);
    }



}
